package com.kzj.controller;

import com.kzj.annotation.OnCreate;
import com.kzj.annotation.OnEdit;
import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.enumerate.RoleEnum;
import com.kzj.common.exception.MyException;
import com.kzj.common.result.Result;
import com.kzj.pojo.College;
import com.kzj.pojo.Course;
import com.kzj.pojo.Teacher;
import com.kzj.pojo.vo.CourseVo;
import com.kzj.service.CourseService;
import com.kzj.service.TeacherService;
import com.kzj.service.jpaRepository.CollegeJpaRepository;
import com.kzj.service.jpaRepository.CourseJpaRepository;
import com.kzj.service.jpaRepository.TeacherJpaRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/course")
@Tag(name="course module",description = "课程管理模块")
public class CourseController {
    @Autowired
    CourseService courseService;
    @PostMapping
    @Operation(summary = "添加课程")
    public Result add(@RequestBody @Validated(OnCreate.class) Course course)
    {
        return courseService.add(course);
    }
    @GetMapping
    @Operation(summary = "获取所有数据")
    public Result getAll()
    {
        return courseService.getAll();
    }
    @GetMapping("/{courseId}")
    @Operation(summary = "通过id获取课程")
    public Result getById(@PathVariable("courseId") Course course)
    {
        return courseService.getById(course);
    }
    @PutMapping
    @Operation(summary = "更新课程")
    public Result updateById(@RequestBody @Validated(OnEdit.class) Course course)
    {
       return courseService.updateById(course);
    }
    @DeleteMapping("/{courseId}")
    @Operation(summary = "通过id删除课程")
    public Result deleteById(@PathVariable("courseId") Long courseId)
    {
        return courseService.deleteById(courseId);
    }
    @GetMapping("/teacher")
    public Result getCourseByTeacher(HttpSession session){
        Long teacherId=(Long)session.getAttribute(GlobalConstant.ID);
        if(RoleEnum.TEACHER!=session.getAttribute(GlobalConstant.ROLE))
            throw new RuntimeException("角色异常");
        return courseService.getCourseByTeacherId(teacherId);
    }
}
